package com.report.excel;

import com.report.SpringExpressionEvaluator;
import com.report.TemplateContext;
import com.report.TemplateEngine;
import com.report.TemplateType;
import org.jxls.transform.Transformer;
import org.jxls.util.JxlsHelper;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class ExcelTemplateEngine implements TemplateEngine {

    @Override
    public void processTemplate(InputStream template, OutputStream output, TemplateContext context) throws IOException {

        JxlsHelper helper = JxlsHelper.getInstance();
        helper.setFullFormulaRecalculationOnOpening(true);

        Transformer transformer = helper.createTransformer(template, output);
        transformer.getTransformationConfig().buildExpressionNotation("{{", "}}");
        SpringExpressionEvaluator evaluator = new SpringExpressionEvaluator();
        transformer.getTransformationConfig().setExpressionEvaluator(evaluator);

        helper.processTemplate(context.getInnerContext(), transformer);
        transformer.dispose();
    }

    @Override
    public TemplateType getTemplateType() {
        return TemplateType.EXCEL;
    }

}
